package com.wxw.getapp.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import com.wxw.getapp.R
import com.wxw.getapp.adapter.Setapplist
import kotlinx.android.synthetic.main.activity_set.*
import java.io.File




class SetActivity : AppCompatActivity(),View.OnClickListener {

    var mFileList = ArrayList<File>()
    var adapter = Setapplist(this)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set)
        initview()
        avi.show()
        Thread(appList()).start()

    }


    inner class  appList : Runnable{
        override fun run() {
            //打开指定目录，显示项目说明书列表，供用户选择
            var PATH = Environment.getExternalStorageDirectory().toString() + "/"
            val specItemDir = File(PATH + "提取APP")
            if (!specItemDir.exists()) {
                specItemDir.mkdir()
            }


            if (!specItemDir.exists()) {

            } else {
                //取出文件列表：
                val files = specItemDir.listFiles()
                if (files!=null){

                    Thread.sleep(500)
                    this@SetActivity.runOnUiThread{

                        adapter.setdata(files)
                        adapter.notifyDataSetChanged()

                        avi.hide()
                    }
                }

            }
        }

    }

    private fun initview() {
        back_iv.setOnClickListener(this)
        set_app_lv.adapter = adapter
        path_tv.text = "保存路径>> "+Environment.getExternalStorageDirectory().toString() + "/提取APP/"

//        if (mFileList!=null){
//            adapter.setBeanData(mFileList)
//            adapter.notifyDataSetChanged()
//        }
    }

    override fun onClick(v: View?) {
        when(v){
            back_iv->{
                finish()
            }
        }
    }

    fun getFile(file: File): List<File> {
        val fileArray = file.listFiles()
        for (f in fileArray) {
            if (f.isFile()) {
                mFileList.add(f)
            } else {
                getFile(f)
            }
        }
        return mFileList
    }

}
